package indi.kider.server.applandlord.api;

import indi.kider.common.model.bill.Bill;
import indi.kider.common.model.bill.BillLeaseCancel;
import indi.kider.common.model.bill.RentBillCountQuery;
import indi.kider.server.applandlord.service.related.BalanceBill;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import pers.vankid.common.model.base.CommonResponse;
import pers.vankid.common.utils.page.PageRequest;

/**
 * Created by shixj on 2018/5/30.
 */
@RequestMapping(value = "bill", produces = "application/json;charset=UTF-8")
@RestController
public class BillContrller {
    @Autowired
    private BalanceBill balanceBill;

    /**
     * @api {PUT} bill/task/all
     * @apiGroup app-landlord
     * @apiDescription 退租出账单
     * @apiParamExample 请求样例：
     * http://localhost:9002/app-landlord/bill/task/all
     * {"rentId":1,"waterReading":1.2,"powerReading":1.3}
     * @apiSuccessExample {json} 返回样例:
     * {"status":"202","msg":"保存成功！","data":""}
     */
    @PutMapping("task/all")
    public CommonResponse<String> leaseBalance(@RequestBody BillLeaseCancel billLeaseCancel) {
        return balanceBill.leaseBalance(billLeaseCancel);
    }

    /**
     * @api {POST} bill/rent-bill
     * @apiGroup app-landlord
     * @apiDescription 公寓管理员获取租客缴费信息及租房信息 ( state: 0:所有 1:欠费 默认查询欠费)
     * @apiParamExample 请求样例：
     * http://localhost:9002/app-landlord/bill/rent-bill
     * {"num":"0","size":"8","query":{"state":1}}
     * @apiSuccessExample {json} 返回样例:
     * {"status":"200","msg":"查询成功！","data":{"rows":[{"tenantId":187,"tenantName":"王斌波","tenantTel":"18969079036","cardId":"876678765456786554","housesId":203,"community":"贝壳1号","houseNumb":"101","address":null,"rentId":213,"rentState":3,"rentStateName":"搬离","billPayState":"欠费","arrearage":"5688.190004289150238037109375","delayDays":2,"bills":[{"id":393,"billType":7,"payState":0,"billTime":"2018-06-05 21:39:12","payTime":null,"billAttribution":213,"lastRead":null,"lastReadTime":null,"thisRead":null,"thisReadTime":null,"absoluteNumber":1000.0,"price":1000.0,"total":1000.0,"paySerial":null,"state":null,"payType":null,"taskId":null,"remark":null,"payTenantId":null,"payTenantName":null,"payTenantTel":null},{"id":394,"billType":5,"payState":0,"billTime":"2018-06-05 21:39:12","payTime":null,"billAttribution":213,"lastRead":null,"lastReadTime":null,"thisRead":null,"thisReadTime":null,"absoluteNumber":4000.0,"price":4000.0,"total":4000.0,"paySerial":null,"state":null,"payType":null,"taskId":null,"remark":null,"payTenantId":null,"payTenantName":null,"payTenantTel":null},{"id":409,"billType":1,"payState":0,"billTime":"2018-06-07 10:54:56","payTime":null,"billAttribution":213,"lastRead":100.0,"lastReadTime":"2018-06-05 21:39:12","thisRead":333.0,"thisReadTime":null,"absoluteNumber":233.0,"price":1.5,"total":359.98,"paySerial":null,"state":1,"payType":null,"taskId":1664,"remark":null,"payTenantId":null,"payTenantName":null,"payTenantTel":null},{"id":410,"billType":5,"payState":0,"billTime":"2018-06-07 10:54:56","payTime":null,"billAttribution":213,"lastRead":null,"lastReadTime":null,"thisRead":null,"thisReadTime":null,"absoluteNumber":null,"price":86.9565,"total":260.87,"paySerial":null,"state":1,"payType":null,"taskId":1666,"remark":null,"payTenantId":null,"payTenantName":null,"payTenantTel":null},{"id":411,"billType":3,"payState":0,"billTime":"2018-06-07 10:54:56","payTime":null,"billAttribution":213,"lastRead":null,"lastReadTime":null,"thisRead":null,"thisReadTime":null,"absoluteNumber":null,"price":0.666667,"total":0.67,"paySerial":null,"state":1,"payType":null,"taskId":1668,"remark":null,"payTenantId":null,"payTenantName":null,"payTenantTel":null},{"id":412,"billType":6,"payState":0,"billTime":"2018-06-07 10:54:56","payTime":null,"billAttribution":213,"lastRead":null,"lastReadTime":null,"thisRead":null,"thisReadTime":null,"absoluteNumber":null,"price":66.6667,"total":66.67,"paySerial":null,"state":1,"payType":null,"taskId":1670,"remark":null,"payTenantId":null,"payTenantName":null,"payTenantTel":null}]}],"total":2,"size":2}}
     */
    @PostMapping("rent-bill")
    CommonResponse getRentBillCountPage(@RequestBody PageRequest<RentBillCountQuery> request) {
        return balanceBill.getRentBillCountPage(request);
    }

    /**
     * @api {POST} /list
     * @apiGroup app-landlord
     * @apiDescription 公寓管理员获取相关支付账单 ( state: 1已支付，0未支付)
     * @apiParamExample 请求样例：
     * http://localhost:9002/app-landlord/bill/list
     * {"num":"0","size":"8","query":{"state":1,"billType":-1}}
     * @apiSuccessExample {json} 返回样例:
     * {"status":"200","msg":"查询成功！","data":{"rows":[{"id":1,"billType":1,"payState":1,"billTime":"2018-05-10 08:58:53","payTime":"2018-05-10 08:00:00","billAttribution":1,"lastRead":"22","lastReadTime":"2018-05-10 08:59:02","thisRead":"22","thisReadTime":"2018-05-10 08:59:05","absoluteNumber":"121","price":"1","total":"1","paySerial":"11111","state":null},{"id":2,"billType":2,"payState":1,"billTime":"2018-05-10 08:58:53","payTime":"2018-05-10 08:00:00","billAttribution":1,"lastRead":"22","lastReadTime":"2018-05-10 08:59:02","thisRead":"22","thisReadTime":"2018-05-10 08:59:05","absoluteNumber":"121","price":"1","total":"1","paySerial":"11111","state":null},{"id":3,"billType":3,"payState":1,"billTime":"2018-05-10 08:58:53","payTime":"2018-05-10 08:00:00","billAttribution":1,"lastRead":"22","lastReadTime":"2018-05-10 08:59:02","thisRead":"22","thisReadTime":"2018-05-10 08:59:05","absoluteNumber":"121","price":"1","total":"1","paySerial":"11111","state":null},{"id":4,"billType":4,"payState":1,"billTime":"2018-05-10 08:58:53","payTime":"2018-05-10 08:00:00","billAttribution":1,"lastRead":"22","lastReadTime":"2018-05-10 08:59:02","thisRead":"22","thisReadTime":"2018-05-10 08:59:05","absoluteNumber":"121","price":"1","total":"1","paySerial":"11111","state":null},{"id":5,"billType":5,"payState":1,"billTime":"2018-05-10 08:58:53","payTime":"2018-05-10 08:00:00","billAttribution":1,"lastRead":"22","lastReadTime":"2018-05-10 08:59:02","thisRead":"22","thisReadTime":"2018-05-10 08:59:05","absoluteNumber":"121","price":"1","total":"1","paySerial":"11111","state":null},{"id":6,"billType":6,"payState":1,"billTime":"2018-05-10 08:58:53","payTime":"2018-05-10 08:00:00","billAttribution":1,"lastRead":"22","lastReadTime":"2018-05-10 08:59:02","thisRead":"22","thisReadTime":"2018-05-10 08:59:05","absoluteNumber":"121","price":"1","total":"1","paySerial":"11111","state":null},{"id":7,"billType":7,"payState":1,"billTime":"2018-05-10 08:58:53","payTime":"2018-05-10 08:00:00","billAttribution":1,"lastRead":"22","lastReadTime":"2018-05-10 08:59:02","thisRead":"22","thisReadTime":"2018-05-10 08:59:05","absoluteNumber":"121","price":"1","total":"1","paySerial":"11111","state":null}],"total":7,"size":0}}
     */
    @PostMapping("list")
    public CommonResponse getEnterpriseBills(@RequestBody PageRequest<RentBillCountQuery> request) {
        return balanceBill.getEnterpriseBills(request);
    }

    /**
     * @api {GET} bill/total/title/{rentId}
     * @apiGroup app-landlord
     * @apiDescription 根据rentId获取租客所有已支付未支付总计金额
     * @apiParamExample 请求样例：
     * http://localhost:9002/app-landlord/bill/total/title/1
     * @apiSuccessExample {json} 返回样例:
     * {"status":"200","msg":"查询成功！","data":{"paid":"1","unpaid":"2"}
     */
    @GetMapping("total/title/{rentId}")
    public CommonResponse getTenantBillFinalCount(@PathVariable Long rentId) {
        return balanceBill.getTenantBillFinalCount(rentId);
    }

    /**
     * @api {POST} bill/
     * @apiGroup app-landlord
     * @apiDescription 手动增加账单
     * @apiParamExample 请求样例：
     * http://localhost:9002/bapp-landlord/bill
     * {billType: "1", billAttribution: 87, total: "2", remark: "2"}
     * @apiSuccessExample {json} 返回样例:
     * {"status":"201","msg":"保存成功！","data":{"rows":[{"id":1,"billType":1,"payState":0,"billTime":"2018-05-10 08:58:53","payTime":null,"billAttribution":1,"lastRead":"22","lastReadTime":"2018-05-10 08:59:02","thisRead":"22","thisReadTime":"2018-05-10 08:59:05","absoluteNumber":"121","price":"1","total":"1","paySerial":""}],"total":1,"size":0}}
     */
    @PostMapping("")
    public CommonResponse add(@RequestBody Bill bill) {
        return balanceBill.add(bill);
    }

    /**
     * @api {POST} bill/rent/list
     * @apiGroup app-landlord
     * @apiDescription 根据billAttribution(就是rent表ID)查询账单（分页）
     * @apiParamExample 请求样例：
     * http://localhost:9002/app-landlord/bill/rent/list
     * {"num":"0","size":"8","query":{"billType":-1,"payState":0,"billAttribution":1}}
     * @apiSuccessExample {json} 返回样例:
     * {"status":"200","msg":"查询成功！","data":{"rows":[{"id":1,"billType":1,"payState":0,"billTime":"2018-05-10 08:58:53","payTime":null,"billAttribution":1,"lastRead":"22","lastReadTime":"2018-05-10 08:59:02","thisRead":"22","thisReadTime":"2018-05-10 08:59:05","absoluteNumber":"121","price":"1","total":"1","paySerial":""}],"total":1,"size":0}}
     */
    @PostMapping("rent/list")
    public CommonResponse query(@RequestBody PageRequest<Bill> request) {
        return CommonResponse.buildGetSuccessResponse(balanceBill.query(request));
    }
}
